<style src="./index.scss" lang="scss" scoped rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
    export default {
        mixins: [Vue.prototype.$vFN.Mixins.Base, Vue.prototype.$vFN.Mixins.Edit],
        props: {
            // 使用者window实例 必定义
            appWindow: {
                type: Object,
                default: null,
            },
            categoryId: {
                type: String,
                default: "",
            },
            subCategoryId: {
                type: String,
                default: "",
            },
        },
        computed: {},

        data() {
            return {

                // 初始化状态
                initLoad: false,
                // 编辑类型
                editType: "",

                addForm: {
                    name: "",
                    attrSub: "",
                    propertyValueLst: []
                },


                // 规格值输入值
                search: "",
                // 规格值数据 不存在
                attrList: [],
                // 规格值数据 已存在
                attrListBe: [],
                // 规格值过滤数据
                attrFilterList: [],

                boxSelectVisible: false,
                // 创建框
                boxCreateVisible: false,


                // 多规格已选规格数据
                skuValue: [


                    //     {
                    //     // 商品参数ID
                    //     propertyId: "",
                    //     // 商品参数名称
                    //     propertyName: "",
                    //     // 商品参数值ID
                    //     propertyValueId: "",
                    //     // 商品参数值
                    //     propertyValue: "",



                ],


                specsSubParentItem: null
            };
        },

        methods: {
            validate() {
                let that = this;
                return new Promise((resolve, reject) => {
                    let _is = null
                    that.skuValue.forEach((item, index) => {
                        if (item.propertyValueId == "") {
                            console.error("item",item);
                            
                            _is = index
                        }
                    })
                    if (_is==null) {
                        resolve(true)
                    } else {
                        this.$vFN.Tools.messageError("请选择 ["+that.skuValue[_is].propertyName+"] 的参数值！");
                        resolve(false)
                    }

                });
            },
            getData() {
                let _propValueIdLst = []
                if (this.skuValue.length > 0) {
                    this.skuValue.forEach(item => {
                        _propValueIdLst.push(item.propertyValueId)
                    })
                }
                let _d = {
                    propValueIdLst: _propValueIdLst,

                }

                return _d;
            },
            async install(params_) {
                this.editType = params_.type;
                if (params_.datasource) {
                    this.skuValue = params_.datasource.propLst

                }
                this.initLoad = true;
                this.returnData();
            },
            async uninstall() {
                this.skuValue = [];
                this.attrList = [];
                this.attrListBe = [];
                this.attrFilterList = [];
                this.editType = "";
                this.search = "";
                this.boxSelectVisible = false;
                this.boxCreateVisible = false;
                this.addForm = {
                    name: "",
                    attrSub: "",
                    attributeValueLst: []
                };
                this.specsSubParentItem = null
            },


            // ————
            // 获取 参数
            async getSpecsData() {


                let _data = await this.$vFN.Http.post({
                    url: "mall-site-portal/product/listProp",
                    data: {
                        categoryId: this.subCategoryId,
                    },
                },this.appWindow);
                if (_data._ok) {
                    // 不存在
                    let _d1 = [];
                    // 已存在
                    let _d2 = [];
                    // 过滤数据源
                    let _dd = this.$vFN.Tools.givenTreeFilter({
                        data: _data.data,
                        parentKeys: {
                            label: "name",
                            value: "id"
                        },
                        subsetKeys: {
                            label: "propertyValue",
                            value: "id"
                        },
                        subsetName: "propValueLst",
                    })

                    _dd.forEach((item) => {
                        let _is = 0;
                        this.skuValue.forEach((iitem) => {
                            if (item.value == iitem.propertyId) {
                                _is += 1;
                            }
                        });

                        if (_is <= 0) {
                            _d1.push(item);
                        } else {
                            _d2.push(item);
                        }
                    });
                    return {
                        d1: _d1,
                        d2: _d2,
                    }
                }
            },

            // 选择 参数
            async showSpecsBaseBox() {

                let _dd = await this.getSpecsData();
                this.$vFN.Directives.SelectGoodsSpecs({
                    appWindow:this.appWindow,
                    textTitle: "参数",
                    model: "extend",
                    list: _dd.d1,
                    listBe: _dd.d2,
                    onSuccess: (r) => {
                        this.selectSpecsBaseBox(r);
                    },
                    onCreate: async (r, t) => {

                        let _propertyValueLst = r.values.map(item => {
                            return {
                                propertyValue: item.value
                            }
                        })
                        let _data = await this.$vFN.Http.post({
                            url: "mall-site-portal/product/createProp",
                            data: {
                                categoryId: this.subCategoryId,
                                name: r.name,
                                propValueLst: _propertyValueLst,
                            }
                        },this.appWindow);
                        if (_data._ok) {
                            this.$vFN.Tools.messageSuccess("创建成功");
                            let _dd3 = await this.getSpecsData()
                            t.updateData({
                                list: _dd3.d1,
                                listBe: _dd3.d2,
                            })
                        }

                    },
                    onDelete: async (r, t) => {

                        let _data = await this.$vFN.Http.post({
                            url: "mall-site-portal/product/delProp",
                            data: {
                                ids: [r.item.value],
                            },
                        },this.appWindow);
                        if (_data._ok) {
                            this.$vFN.Tools.messageSuccess("删除成功");
                            let _dd3 = await this.getSpecsData()
                            t.updateData({
                                list: _dd3.d1,
                                listBe: _dd3.d2,
                            })
                        }

                    },
                })
            },

            // 设置 参数
            selectSpecsBaseBox(item_) {
                this.skuValue.push({
                    // 商品参数ID
                    propertyId: item_.value,
                    // 商品参数名称
                    propertyName: item_.label,
                    // 商品参数值ID
                    propertyValueId: "",
                    // 商品参数值
                    propertyValue: "",
                })
            },

            // 移除 参数
            removeSpecBase(item_, index_) {


                this.$delete(this.skuValue, index_);

            },

            // ————
            // 获取 参数值
            async getSpecsSubData() {

                let _data = await this.$vFN.Http.post({
                    url: "mall-site-portal/product/listProp",
                    data: {
                        categoryId: this.subCategoryId,
                    },
                },this.appWindow);
                if (_data._ok) {
                    // 不存在
                    let _d1 = [];
                    // 已存在
                    let _d2 = [];
                    // 过滤数据源
                    let _dd = this.$vFN.Tools.givenTreeFilter({
                        data: _data.data,
                        parentKeys: {
                            label: "name",
                            value: "id"
                        },
                        subsetKeys: {
                            label: "propertyValue",
                            value: "id"
                        },
                        subsetName: "propValueLst",
                    })


                    _dd.forEach((item) => {
                        if (this.specsSubParentItem.propertyId == item.value) {
                            item.children.forEach((iiitem) => {
                                let _is = 0;

                                if (iiitem.value == this.specsSubParentItem.propertyValueId) {
                                    _is += 1;
                                }

                                if (_is <= 0) {
                                    _d1.push(iiitem);
                                } else {
                                    _d2.push(iiitem);
                                }
                            });
                        }
                    });

                    return {
                        d1: _d1,
                        d2: _d2,
                    }
                }
            },

            // 选择 参数值
            async showSpecsSubBox(item_) {

                this.specsSubParentItem = item_;

                let _dd = await this.getSpecsSubData()

                this.$vFN.Directives.SelectGoodsSpecs({
                    appWindow:this.appWindow,
                    textTitle: "参数值",
                    list: _dd.d1,
                    listBe: _dd.d2,
                    onCancel: (r) => {
                        this.specsSubParentItem = null
                    },
                    onSuccess: (r) => {
                        this.selectSpecsSubBaseBox(r);
                    },
                    onCreate: async (r) => {
                        let _data = await this.$vFN.Http.post({
                            url: "mall-site-portal/product/createPropValue",
                            data: {
                                propertyValue: r.value,
                                propertyId: this.specsSubParentItem.propertyId,
                            },
                        },this.appWindow);
                        if (_data._ok) {
                            this.selectSpecsSubBaseBox({
                                value: _data.data.id,
                                label: _data.data.propertyValue,
                            });
                        }
                    },
                    onDelete: async (r, t) => {
                        let _data = await this.$vFN.Http.post({
                            url: "mall-site-portal/product/delPropValue",
                            data: {
                                ids: [r.item.value],
                            },
                        },this.appWindow);
                        if (_data._ok) {
                            this.$vFN.Tools.messageSuccess("删除成功");
                            let _dd3 = await this.getSpecsSubData()
                            t.updateData({
                                list: _dd3.d1,
                                listBe: _dd3.d2,
                            })
                        }

                    },
                })

            },

            // 设置 参数值
            selectSpecsSubBaseBox(item_) {

                this.skuValue.forEach((item, index) => {
                    if (this.specsSubParentItem.propertyId == item.propertyId) {
                        this.skuValue[index].propertyValueId = item_.value;
                        this.skuValue[index].propertyValue = item_.label;
                    }
                });

            },

            // 移除 参数值
            removeSpecSub(item_, index_, iitem_, iindex_) {
                this.$delete(this.skuValue[index_].specsValueLst, iindex_);
            },
            returnData() {
                this.$emit('change', {
                    data: this.getData(),
                    load: this.initLoad
                })
            },
        },
        mounted() {},
        beforeCreate() {},
        watch: {
            "form": {
                handler(newName, oldName) {
                    this.returnData();
                },
                immediate: true,
                deep: true,
            }
        },
    };
</script>